﻿using UnityEngine;
using System.Collections;
using DICE_GS;

public enum SHOPTYPE
{
    NONE,
    ITEM=0,
    CASH,
    STAMINA,
    GOLD,
    UNIT,
	SKILL,
	CHARACTER
};

public class ShopMainManager : LTEventListenerC {
    enum CharacterName
    {
        Dawool = 1,
        Issad,
        Mana,
        Teharil,
        Fanny,
        Ruda
    };

    public static ShopMainManager singleton;

    [HideInInspector]
    public static SHOPTYPE currentShopType = SHOPTYPE.ITEM;

    public AddShopItemUnits itemGrid;

    public GameObject prevTabObject;

    public GameObject NormalGachaButtonObject;
    public GameObject PremiumGachaButtonObject;

    public TopInfoBar topInfoBar;
	
	public GameObject[] tabObjects;

    public GameObject characterDetailWindow;
    public UISprite detailCharacterImage;
    public UILabel detailCharacterName;
    public UILabel detailCharacterDesc;
    public UISprite detailCharacterSkill1Icon;
    public UILabel detailCharacterSkill1Attribute;
    public UILabel detailCharacterSkill1Name;
    public UILabel detailCharacterSkill1Desc;
    public UISprite detailCharacterSkill2Icon;
    public UILabel detailCharacterSkill2Attribute;
    public UILabel detailCharacterSkill2Name;
    public UILabel detailCharacterSkill2Desc;

    public GameObject skillDetailWindow;
    public UISprite detailSkillIcon;
    public UILabel detailSkillAttribute;
    public UILabel detailSkillName;
    public UILabel detailSkillDesc;
     

    void Awake()
    {
        if( singleton == null )
            singleton = this;
    }

	// Use this for initialization
	void Start () {
        SelectTab(currentShopType);			
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    void TabClicked(GameObject tabObject)
    {
        Debug.LogError(tabObject.name);
        LoadingWindow.window.ShowShortLoading();
        if (prevTabObject != null)
        {
            Transform prevTabObjectTransform = prevTabObject.transform;
            //			prevTabObjectTransform.localPosition = new Vector3(prevTabObjectTransform.localPosition.x, 260f, prevTabObjectTransform.localPosition.z);
            Transform prevTabObjectBacakgroundTransform = prevTabObjectTransform.FindChild("Background");
            prevTabObjectBacakgroundTransform.GetComponent<UISprite>().spriteName = "Shop_TabBtn02";
            Vector3 pos = prevTabObjectBacakgroundTransform.localPosition;
            pos.y = 3f;
            prevTabObjectBacakgroundTransform.localPosition = pos;
            prevTabObjectTransform.FindChild("Com_EnablePoint").gameObject.SetActive(false);
            prevTabObjectTransform.FindChild("Icon").gameObject.SetActive(false);
            prevTabObjectTransform.FindChild("Label").GetComponent<UILabel>().color = new Color(163f / 255f, 142f / 255f, 99f / 255f);
            prevTabObjectTransform.FindChild("Label").GetComponent<UILabel>().effectStyle = UILabel.Effect.Shadow;
            prevTabObjectTransform.FindChild("Label").GetComponent<UILabel>().effectColor = Color.black;
            //	        prevTabObjectBacakgroundTransform.GetComponent<UISprite>().alpha = 0.75f;
        }
        Transform tabObjectTransform = tabObject.transform;
        //		tabObjectTransform.localPosition = new Vector3(tabObjectTransform.localPosition.x, 265f, tabObjectTransform.localPosition.z);
        Transform tabObjectBacakgroundTransform = tabObjectTransform.FindChild("Background");
        tabObjectTransform.FindChild("Com_EnablePoint").gameObject.SetActive(true);
        tabObjectTransform.FindChild("Icon").gameObject.SetActive(true);
        tabObjectTransform.FindChild("Label").GetComponent<UILabel>().color = new Color(48f / 255f, 42f / 255f, 30f / 255f);
        tabObjectTransform.FindChild("Label").GetComponent<UILabel>().effectStyle = UILabel.Effect.None;
        tabObjectBacakgroundTransform.GetComponent<UISprite>().spriteName = "Shop_TabBtn01";
        {
            Vector3 pos = tabObjectBacakgroundTransform.localPosition;
            pos.y = 1f;
            tabObjectBacakgroundTransform.localPosition = pos;
        }
        //        tabObjectBacakgroundTransform.GetComponent<UISprite>().alpha = 1.0f;
        prevTabObject = tabObject;
        if (tabObject.name.Contains("_1") == true)
        {
            currentShopType = SHOPTYPE.ITEM;
            itemGrid.SetItems(currentShopType);

            //            if (NormalGachaButtonObject.activeSelf)
            //                NormalGachaButtonObject.SetActive(false);
            //            if (PremiumGachaButtonObject.activeSelf)
            //                PremiumGachaButtonObject.SetActive(false);
        }
        else if (tabObject.name.Contains("_2") == true)
        {
            currentShopType = SHOPTYPE.CASH;
            itemGrid.SetItems(currentShopType);

            //            if (NormalGachaButtonObject.activeSelf)
            //                NormalGachaButtonObject.SetActive(false);
            //            if (PremiumGachaButtonObject.activeSelf)
            //                PremiumGachaButtonObject.SetActive(false);
        }
        else if (tabObject.name.Contains("_3") == true)
        {
            currentShopType = SHOPTYPE.SKILL;
            if (MyInfo.Instance.MySkillList.Count == 0)
            {
                LoadingWindow.window.ShowShortLoading();
                RestAPI.singleton.GetMySkillList(RestCallback);
                return;
            }
            else
                itemGrid.SetItems(currentShopType);

            //            if (NormalGachaButtonObject.activeSelf)
            //                NormalGachaButtonObject.SetActive(false);
            //            if (PremiumGachaButtonObject.activeSelf)
            //                PremiumGachaButtonObject.SetActive(false);
        }
        else if (tabObject.name.Contains("_4") == true)
        {
            currentShopType = SHOPTYPE.GOLD;
            itemGrid.SetItems(currentShopType);

            //            if (NormalGachaButtonObject.activeSelf)
            //                NormalGachaButtonObject.SetActive(false);
            //            if (PremiumGachaButtonObject.activeSelf)
            //                PremiumGachaButtonObject.SetActive(false);
        }
        else if (tabObject.name.Contains("_5") == true)
        {
            currentShopType = SHOPTYPE.CHARACTER;
            if (MyInfo.Instance.MyCharacterList.Count == 0)
            {
                LoadingWindow.window.ShowShortLoading();
                RestAPI.singleton.GetMyCharacterList(RestCallback);
                return;
            }
            else
                itemGrid.SetItems(currentShopType);
            //            NormalGachaButtonObject.SetActive(true);
            //            PremiumGachaButtonObject.SetActive(true);
        }
        LoadingWindow.window.HideLoading();
    }
	
	public void SelectTab(SHOPTYPE type)
    {
        foreach (GameObject tabObject in tabObjects)
        {
            Transform tabObjectTransform = tabObject.transform;
            //			tabObjectTransform.localPosition = new Vector3(tabObjectTransform.localPosition.x, 260f, tabObjectTransform.localPosition.z);
            Transform tabObjectBacakgroundTransform = tabObjectTransform.FindChild("Background");
            tabObjectBacakgroundTransform.GetComponent<UISprite>().spriteName = "Shop_TabBtn02";
            Vector3 pos = tabObjectBacakgroundTransform.localPosition;
            pos.y = 3f;
            tabObjectBacakgroundTransform.localPosition = pos;
            tabObjectTransform.FindChild("Com_EnablePoint").gameObject.SetActive(false);
            tabObjectTransform.FindChild("Icon").gameObject.SetActive(false);
            tabObjectTransform.FindChild("Label").GetComponent<UILabel>().color = new Color(163f / 255f, 142f / 255f, 99f / 255f);
            tabObjectTransform.FindChild("Label").GetComponent<UILabel>().effectStyle = UILabel.Effect.Shadow;
            tabObjectTransform.FindChild("Label").GetComponent<UILabel>().effectColor = Color.black;
            //	        tabObjectBacakgroundTransform.GetComponent<UISprite>().alpha = 0.75f;
        }

        Transform trans = tabObjects[(int)type].transform;
        //		trans.localPosition = new Vector3(trans.localPosition.x, 265f, trans.localPosition.z);
        Transform bacakgroundTrans = trans.FindChild("Background");
        bacakgroundTrans.GetComponent<UISprite>().spriteName = "Shop_TabBtn01";
        {
            Vector3 pos = bacakgroundTrans.localPosition;
            pos.y = 1f;
            bacakgroundTrans.localPosition = pos;
        }
        trans.FindChild("Com_EnablePoint").gameObject.SetActive(true);
        trans.FindChild("Icon").gameObject.SetActive(true);
        trans.FindChild("Label").GetComponent<UILabel>().color = new Color(48f / 255f, 42f / 255f, 30f / 255f);
        trans.FindChild("Label").GetComponent<UILabel>().effectStyle = UILabel.Effect.None;
        //		bacakgroundTrans.GetComponent<UISprite>().alpha = 1.0f;

        itemGrid.SetItems(type);

        prevTabObject = trans.gameObject;

        Debug.LogError(trans.FindChild("Label").GetComponent<UILabel>().color.r);
    }

    public void RefreshTopInfos()
    {
        topInfoBar.UpdateInfos();
    }

	public void RestCallback(Hashtable returnData)
	{
		LoadingWindow.window.HideLoading();
		switch((RestAPIProtocol)returnData["protocol"])
		{
		default:
			itemGrid.SetItems(currentShopType);
			break;
		}
	}

    public override void OnEvent(int eventID)
    {
        base.OnEvent(eventID);

        if (eventID < 100000)
        {
            CCharacterData charData = CCharacterDataManager.Instance.GetCharacterData(eventID);
            if (charData != null)
            {
                detailCharacterName.text = CStringTableManager.Instance.GetItemByName(charData.name_string);
                detailCharacterDesc.text = CStringTableManager.Instance.GetItemByName(charData.desc_string);
                detailCharacterImage.spriteName = ((CharacterName)eventID).ToString();
                CSkillData skillData = CSkillDataManager.Instance.FindSkillData(charData.skill_01);
                if (skillData != null)
                {
                    detailCharacterSkill1Icon.spriteName = skillData.icon_name;
                    //detailCharacterSkill1Attribute.text = CStringTableManager.Instance.GetItemByName(
                    detailCharacterSkill1Name.text = CStringTableManager.Instance.GetItemByName(skillData.name_string);
                    detailCharacterSkill1Desc.text = CStringTableManager.Instance.GetItemByName(skillData.desc_string);
                }
                skillData = CSkillDataManager.Instance.FindSkillData(charData.skill_02);
                if (skillData != null)
                {
                    detailCharacterSkill2Icon.spriteName = skillData.icon_name;
                    //detailCharacterSkill1Attribute.text = CStringTableManager.Instance.GetItemByName(
                    detailCharacterSkill2Name.text = CStringTableManager.Instance.GetItemByName(skillData.name_string);
                    detailCharacterSkill2Desc.text = CStringTableManager.Instance.GetItemByName(skillData.desc_string);
                }

                characterDetailWindow.SetActive(true);
            }
        }
        else if (eventID > 100000)
        {
            CSkillData skillData = CSkillDataManager.Instance.FindSkillData(eventID);
            if (skillData != null)
            {
                detailSkillIcon.spriteName = skillData.icon_name;
                //detailSkillAttribute.text = 
                detailSkillName.text = CStringTableManager.Instance.GetItemByName(skillData.name_string);
                detailSkillDesc.text = CStringTableManager.Instance.GetItemByName(skillData.desc_string);
                skillDetailWindow.SetActive(true);
            }
        }
    }

    void HideCharacterWindow()
    {
        characterDetailWindow.SetActive(false);
    }

    void HideSkillDetailWindow()
    {
        skillDetailWindow.SetActive(false);
    }
}
